package cn.com.nes.site.service.templatedesign.impl;


import cn.com.nes.common.util.Globals;
import cn.com.nes.common.util.OssUtil;
import cn.com.nes.common.util.ParameterUtil;
import cn.com.nes.mybatis.agent.templatedesign.entity.HDevicemodel;
import cn.com.nes.mybatis.agent.templatedesign.mapper.HDevicemodelMapper;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.vo.design.deviceModelVo;
import cn.com.nes.site.service.templatedesign.HDevicemodelService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *  业务实现类
 *
 * @author 
 * @since 2023-09-26
 */
@Service
public class HDevicemodelServiceImpl  implements HDevicemodelService {
    @Resource
    private HDevicemodelMapper hDevicemodelMapper;

    /**
     * 添加设备模板
     * @param req
     * @return
     * @throws IOException
     */
    @Override
    public int addHouseDeviceModel(DataReq req) throws Exception {
       String sbcj=ParameterUtil.getString(req,"sbcj");
//       插入设备模板时，设备型号id字段sbxh_id和设备型号名字sbxhname内容相同
       String sbxh_id=ParameterUtil.getString(req,"sbxh_id");
       String devicedesignfilename=ParameterUtil.getString(req,"devicedesignfilename");
       String devicedesignpath=devicedesignfilename;
       Integer createUid=ParameterUtil.getInteger(req,"createUid");
       String createTime=ParameterUtil.getString(req,"createTime");
       String createName=ParameterUtil.getString(req,"createName");
//       在插入设备模板前先查询一下同样一个设备厂商的设备型号是否存在，如果存在返回9，操作失败。
        List<Map<String,Object>> list=hDevicemodelMapper.selectSbcjAndSbxh_id(sbcj,sbxh_id);
        int count=0;
        if (list.size()==0){
            count=hDevicemodelMapper.addHouseDeviceModel(sbcj,sbxh_id,devicedesignpath,devicedesignfilename,createUid,createTime,createName);
        }

        return count;

    }

    /**
     * 编辑设备模板
     * @param req
     * @return
     * @throws IOException
     */
    @Override
    public int updateHouseDeviceModel(DataReq req) throws Exception{
        int id=ParameterUtil.getInteger(req,"id");
        String sblx_id=ParameterUtil.getString(req,"sblx_id");
        String sblxname=ParameterUtil.getString(req,"sblxname");
        String sbcj=ParameterUtil.getString(req,"sbcj");
        String sbcjname=ParameterUtil.getString(req,"sbcjname");
        String sbxh_id=ParameterUtil.getString(req,"sbxh_id");
        String sbxhname=ParameterUtil.getString(req,"sbxhname");
        String modifyUid=ParameterUtil.getString(req,"modifyUid");
        String devicedesignfilename=ParameterUtil.getString(req,"devicedesignfilename");
        String devicedesignpath=ParameterUtil.getString(req,"devicedesignpath");
        String createName=ParameterUtil.getString(req,"createName");
       int count = hDevicemodelMapper.updateHouseDeviceModel(id,sblx_id,sbcj,sbxh_id,modifyUid,devicedesignfilename,devicedesignpath,createName);
        return count;
    }

    /**
     * 删除设备模板
     * @param req
     * @return
     */
    @Override
    public int deleteHouseDeviceModelById(DataReq req) throws Exception{
        int id=ParameterUtil.getInteger(req,"id");
        int count = hDevicemodelMapper.deleteHouseDeviceModelById(id);
        int creatUid= Integer.parseInt(req.getUinfo().getCreateUid());
        return count;
    }

    /**
     * 根据设备厂家id查询设备型号的名字和设备型号的id
     * @param req
     * @return
     */
    @Override
    public List<Map<String, Object>> selectSbxh_idAndSbxhname(DataReq req)throws Exception {
        String sbcj=ParameterUtil.getString(req,"sbcj");
        List<Map<String,Object>> list=(List<Map<String, Object>>) hDevicemodelMapper.selectSbxh_idAndSbxhname(sbcj);
        return list;
    }

    /**
     * 查询设备模板表所有数据
     * @param req
     * @return
     */
    @Override
    public int selectAllList(DataReq req) throws Exception{
        int count=hDevicemodelMapper.selectAllList();
        return count;
    }



    /**
     * 查询设备模板
     * @param req
     * @return
     */
    @Override
    public List<Map<String, Object>> selectHouseDeviceModel(DataReq req)throws Exception {
        //分页查询 pageid*pagesize-pagesize
        Integer pageid=ParameterUtil.getInteger(req,"pageid");
        Integer pagesize=ParameterUtil.getInteger(req,"pagesize");
        int offSet=pageid*pagesize-pagesize;
        Integer id=ParameterUtil.getInteger(req,"id");
        String sbcj=ParameterUtil.getString(req,"sbcj");
        String sbxh_id=ParameterUtil.getString(req,"sbxh_id");
        List<Map<String,Object>> list=(List<Map<String, Object>>) hDevicemodelMapper.selectHouseDeviceModel(id,sbcj,sbxh_id,offSet,pagesize);
        String ossUrl="http://" + Globals.bucketName + "." + Globals.endpoint_nohttp + "/";
        for (int i=0;i< list.size();i++){
            if (list.get(i).get("devicedesignfilename")!=null){
                List<String> devicepicfilenameList=new ArrayList<>();
                String strdevicepicfilename=(String)list.get(i).get("devicedesignfilename");
                String[] devicepicfilenameArr=strdevicepicfilename.split(";");
                for (int i1=0;i1<devicepicfilenameArr.length;i1++){
                    String oss_devicepicfilename=OssUtil.getImgSignURL(devicepicfilenameArr[i1],0);
                    devicepicfilenameList.add(oss_devicepicfilename);
                }
                list.get(i).put("devicedesignpath",devicepicfilenameList);
            }
            if (list.get(i).get("createTime")!=null){
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
                String createTime=simpleDateFormat.format(list.get(i).get("createTime"));
                list.get(i).put("createTime",createTime);
            }
        }
        return list;



    }
//  通过实体类传参
// ------------------------------------------------------------------------------------------------------------------------------------------------------------
    /**
     * 通过实体类获取id更新设备模板
     * @param hDevicemodel
     * @return
     */
    @Override
    public int updateDeviceModel(HDevicemodel hDevicemodel) throws Exception{
     hDevicemodel.setDevicedesignpath(hDevicemodel.getDevicedesignfilename());
     Integer count=hDevicemodelMapper.updateById(hDevicemodel);
     if (count!=0&&count!=0)
         return count;
     return 0;
    }

    /**
     * 通过实体类添加设备模板
     * @param hDevicemodel
     * @return
     */
    @Override
    public int addDeviceModel(HDevicemodel hDevicemodel)throws Exception {
        hDevicemodel.setDevicedesignpath(hDevicemodel.getDevicedesignfilename());
        Integer count=hDevicemodelMapper.updateById(hDevicemodel);
        if (count!=0&&count!=0)
            return count;
        return 0;
    }

    /**
     * 通过实体类获取id删除设备模板
     * @param hDevicemodel
     * @return
     */
    @Override
    public int deleteDeviceModelById(HDevicemodel hDevicemodel) throws Exception{
        Integer count=hDevicemodelMapper.updateById(hDevicemodel);
        if (count!=0&&count!=null)
            return count;
        return 0;
    }

    /**
     * 查询设备类型id和设备类型name
     * @param hDevicemodel
     * @return
     */
    @Override
    public List<Map<String,Object>> selectSbxhIdAndName(HDevicemodel hDevicemodel) {
        List<Map<String,Object>> list =hDevicemodelMapper.selectSbxhIdAndName(hDevicemodel);
        return list;
    }

    /**
     * 查询设备模板
     * @param modelVo
     * @return
     * @throws Exception
     */
    @Override
    public Page<Map<String, Object>> selectDeviceModel(deviceModelVo modelVo)  {
//        Page<Map<String,Object>> page=hDevicemodelMapper.s;
        return null;
    }
}
